/**
AirwayPathfinder for X-Plane Flight Simulator. Calculates the shortest path between two NavAids. 
Copyright (C) 2011  Nils Buletta

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/

package de.buletta.airwaypathfinder;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Point2D;

import org.jdesktop.swingx.mapviewer.Waypoint;

/**
 * Listens for mouse clicks on the map.
 * 
 * @author Nils Buletta
 * @version 0.1
 */
public class MapMouseListener implements MouseListener {
	private Gui g;

	public MapMouseListener(Gui g) {
		this.g = g;
	}

	/**
	 * Reacts to mouse clicks. Only mouse button 3 has an effect in this mouse
	 * listener.
	 */
	@Override
	public void mouseClicked(MouseEvent evt) {

		// if left mouse button is clicked
		if (evt.getButton() == MouseEvent.BUTTON3) {
			Rectangle bounds = g.mapKit.getMainMap().getViewportBounds();

			Point2D vorPoint;
			Rectangle vorBounds;
			int x, y;

			for (Waypoint w : g.holder.getNavsSet()) {
				vorPoint = g.mapKit
						.getMainMap()
						.getTileFactory()
						.geoToPixel(((EarthNav) w).getPosition(),
								g.mapKit.getMainMap().getZoom());
				x = (int) (vorPoint.getX() - bounds.getX());
				y = (int) (vorPoint.getY() - bounds.getY());

				vorBounds = new Rectangle(x - 10, y - 10, 20, 20);

				// g.mapKit.getMainMap().getOverlayPainter().paint(arg0, arg1,
				// arg2, arg3)

				if (vorBounds.contains(evt.getPoint())) {
					// System.out.println(((EarthNav)w).getIdentifier() +
					// " clicked. Has " + ((EarthNav)w).getConnections().size()
					// + " connections");
					if (((EarthNav) w).getConnections().size() > 0) {
						g.selectWaypoint((EarthNav) w);
						return;
					}

				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
